package com.ldy.job.utils;

import com.ldy.job.ReportUtilsApplication;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import static com.ldy.job.listener.NoModelDataListener.getLastMonth;
import static com.ldy.job.utils.ExcelUtils.RESSTR_YEWUYUNXINGFENXI;

public class WriteDoc {
    public static void writeDoc(Map<String, String> params) {
        try {
            String templatePath = ReportUtilsApplication.DOC_PATH + "\\模板\\template.doc";
            InputStream is = new FileInputStream(templatePath);
            OutputStream os = null;
            HWPFDocument doc = new HWPFDocument(is);
            Range range = doc.getRange();
            for (Map.Entry<String, String> entry : params.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                RESSTR_YEWUYUNXINGFENXI = RESSTR_YEWUYUNXINGFENXI.replace("${" + key + "}", value);
                range.replaceText("${" + key + "}", value);
                os = new FileOutputStream(ReportUtilsApplication.DOC_PATH + "\\生成\\“单一窗口”推广应用月报(" + getLastMonth() + "月).doc");
                //把doc输出到输出流中
                doc.write(os);
            }
            if (os != null) {
                os.close();
            }
            is.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}